package TUILayer;

import ControlLayer.*;
import ModeLayer.*;
import java.util.Scanner;
/**
 * Example to test of try-catch
 * 
 * @author (Kis Boisen Hansen) 
 * @version (2014.04.03)
 */
public class ForestTUI
{
    
 public void mainMenuStart()
   {
        boolean exit=false;
        while (!exit) //! means while exit not is true (that is: false)
        {
            int choise = writeMainMenu();
            switch (choise)
            {
                case 1:
                    createForest();
                    break;  
                case 2:
                    createOwner();
                    break;
                case 3:
                    addOwnerToForest();
                    
                    break;
                case 4:
                    showForest();
                    break;
                case 5:
                    showOwner();
                    break; 
                case 6:
                    exit = true;
                    break;
    
            }//end switch
                
        }//end while
    }//end start
    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String wait = keyboard.nextLine();
    }
     private int writeMainMenu()
    {       
            // creates a keyboard object to read input
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Forest MainMenu ***");
            System.out.println(" (1) Register a new forest");
            System.out.println(" (2) Registrer a new owner");
            System.out.println(" (3) Register an owner to a forest");
            System.out.println(" (4) Show forest");
            System.out.println(" (5) Show owner");
            System.out.println(" (6) Close the program");
            
            System.out.print("\n Make your choise: ");
            
            int choise = keyboard.nextInt();
            
            return choise;
    }
   
    
   
    /*
     * Method to read the information about the a new forest
     * 
     */
    private void createForest()
    { 
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f Register information about a forest");
        System.out.println("Insert the forest name: ");
        String fName = keyboard.nextLine();
        System.out.println("Insert the location: ");
        String fLocation = keyboard.nextLine();
        double squareMeter = 0;
        while (squareMeter < 1){
            squareMeter = getDouble(" square meter ");
        }
        System.out.println("Insert the nature of tree: ");
        String fNatureOfTree = keyboard.nextLine();
        double pricePrSquareMeter = 0;
        while (pricePrSquareMeter < 1){
            pricePrSquareMeter = getDouble(" price pr. square meter ");
        }
        ForestOwnerCtr ctrForestOwner = new ForestOwnerCtr ();
        ctrForestOwner.addForest(fName, fLocation,squareMeter, fNatureOfTree, pricePrSquareMeter);
        System.out.println("The forest has been registred");
        pause();
        
       
        
        
        
    }
    private double getDouble(String text)
    {
        Scanner keyboard = new Scanner(System.in);
        keyboard = new Scanner(System.in);
        double inputDouble = 0;
        try{
           System.out.println("Insert " + text + " :");
           inputDouble = keyboard.nextDouble();
           return inputDouble;
        }
        catch(Exception e)
        {
            System.out.println(text + " should be a number");
           
        }
        return inputDouble;
    }
    private void createOwner()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f Register information about an owner");
        System.out.println("Insert the owner name: ");
        String oName = keyboard.nextLine();
        System.out.println("Insert the owner address: ");
        String oAddress = keyboard.nextLine();
        System.out.println("Insert the owner phonenumber: ");
        int oPhone = keyboard.nextInt();
        ForestOwnerCtr ctrForestOwner = new ForestOwnerCtr ();
        ctrForestOwner.addOwner(oName, oAddress, oPhone);
        System.out.println("The owner has been registred");
        pause();
        
    }
    private void addOwnerToForest()
    {
         Scanner keyboard = new Scanner(System.in);
        System.out.println("\f Add an owner to a forest");
        System.out.println("Insert the forest name: ");
        String fName = keyboard.nextLine();
        System.out.println("Insert the owner name: ");
        String oName = keyboard.nextLine();
        ForestOwnerCtr ctrForestOwner = new ForestOwnerCtr ();
        try{
            ctrForestOwner.addOwnerToForest(oName,fName);
            System.out.println("The forest now have an owner");
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        pause();
        
    }
    private void showForest()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f Show information about forest");
        System.out.println("Insert the forest name: ");
        String fName = keyboard.nextLine();
        ForestOwnerCtr ctrForestOwner = new ForestOwnerCtr ();
        Forest forestObj = ctrForestOwner.findForest(fName);
        if(forestObj != null)
        {
            printForest(forestObj); 
        }
        else{
            System.out.println("The forest was not found");
            pause();
        }
        
    }
    private void showOwner()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f Show information about owner");
        System.out.println("Insert the owner name: ");
        String oName = keyboard.nextLine();
        ForestOwnerCtr ctrForestOwner = new ForestOwnerCtr ();
        Owner ownerObj = ctrForestOwner.findOwner(oName);
        if(ownerObj != null)
        {
            printOwner(ownerObj);
        }
        else{
            System.out.println("Owner not found");
            pause();
        }
    }
    private void printForest(Forest forestObj)
    {
         System.out.println("Forest details:");
         System.out.println("Name        : " + forestObj.getName());
         System.out.println("Location    : " + forestObj.getLocation());
         System.out.println("SquareMetre : " + forestObj.getSquareMetre());
         System.out.println("Nature of Trees: " + forestObj.getNatureOfTrees());
         System.out.println("Price pr. square meter: " + forestObj.getPricePrSquareMetre());
         if(forestObj.getOwner() != null)
         {
             printOwner(forestObj.getOwner());
         }
         else{
                 System.out.println("The forest has no owner");
         }
         pause();  
    }
    private void printOwner(Owner ownerObj)
    {
        System.out.println("Owner details:");
        System.out.println("Name       : " + ownerObj.getOwnerName());
        System.out.println("Address    : " + ownerObj.getAddress());
        System.out.println("Phone number: " + ownerObj.getPhoneNumber());
        pause();
    }
}
